import React, { Component } from 'react';
import { observer, inject } from 'mobx-react';
import { withRouter } from 'react-router-dom';
import {Tools, TimeRangePicker,InputItem, List, Switch,Checkbox,Button,WhiteSpace,Select } from 'weaver-mobile';
import { Browser,SelectPage,BrowserHrm} from 'weaver-mobile-page';
const CheckboxItem = Checkbox.CheckboxItem;
const getLabel = Tools.getLabel;
@inject('addShareStore','userCenterStore')
@withRouter
@observer
export default class EditShare extends Component {
    constructor(props) {
        super(props);
        this.state = {
          date: this.getCanviewTime(),
        };
          this.canViewMinTime="";
          this.canViewMaxTime="";
      }

      getCanviewTime(){
        let canTime = this.props.location.rowData.canViewMinTime;
        let stringResult = canTime.split('~');
        let startTime1 = stringResult[0];
        let endTime1 = stringResult[1];
        let startTime =startTime1.replace(/(^\s*)|(\s*$)/g, "");
        let endTime = endTime1.replace(/(^\s*)|(\s*$)/g, "");
        if(startTime == getLabel('763','不限')){
          startTime =  "";
        }
        if(endTime == getLabel('763','不限')){
          endTime =  "";
        }
        return [startTime, endTime];
      }

      editShare= (e)=>{
        const {  userCenterStore: { userShareSetStore } } = this.props;
        const {myShareList } = userShareSetStore;
        let params = {};
        let rowData =  this.props.location.rowData; 
        params["canViewMinTime"] =  this.canViewMinTime;
        params["canViewMaxTime"] =  this.canViewMaxTime;
        params["shareType"] =rowData.type;
        params["shareTypespan"] =rowData.shareType2span;
        params["id"] = rowData.randomFieldId;
        params["operation"] = "saveEdit";
        const options = {
            url: '/api/blog/shareset/operateBlogShareSet',
            method: 'POST',
            params: params
          };
          Tools.callApi(options).then((src) => {
            if (src.status=="1") {
                myShareList.clearData();
                this.props.history.push({ pathname : '/userCenter/setting/shareSet'})
            }
          });
      }
 
  render() {
    return (
      <div style={{height:'100%'}}>
        <InputItem
            editable
            value={this.props.location.rowData.typespan}
            ref={el => this.autoFocusInst = el}
    >{getLabel('500401','类型') }</InputItem>
        <TimeRangePicker
          mode="date"
          label={getLabel('509722','起止可查看时间') }
          placeholder={[getLabel('24978','开始日期') , getLabel('741','结束日期') ]}
          value={this.state.date}
          onChange={(v) => {
            this.canViewMinTime=v[0];
            this.canViewMaxTime=v[1];  
           }}
        />
         <Button style={{position:'absolute',width:'100%',bottom:'0'}} type="primary" onClick={(e)=>this.editShare(e)} >{getLabel('30986','保存')}</Button><WhiteSpace />
      </div>
    );
  }
}